package com.sx.micowell.framework.model.smartDevice;

import android.annotation.SuppressLint;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.mvvm.base.data.MutableLiveListData;
import com.example.mvvm.base.data.SingleLiveEvent;
import com.example.mvvm.base.vm.BaseViewModel;
import com.example.mvvm.rx.RxBus;
import com.example.mvvm.until.LogUtils;
import com.miloyu.modelnet.network.ApiResponse;
import com.miloyu.modelnet.network.BaseResponse;
import com.miloyu.modelnet.network.HttpClient;
import com.miloyu.modelnet.network.tools.RxUtils;
import com.miloyu.modelnet.response.SmartDeviceTypeVO;
import com.miloyu.modelnet.response.SmartDeviceWrap;
import com.sx.micowell.framework.app.AppConstant;
import com.sx.micowell.framework.tools.EventInfo;

import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;


/**
 * @Author: YY
 * @Time: 2021/9/16
 * @Describe TODO
 */
public class AddDeviceListModel extends BaseViewModel {

    public MutableLiveListData<SmartDeviceTypeVO> deviceTypeData = new MutableLiveListData<>();

    public AddDeviceListModel() {
       getDeviceType();
    }

    @Override
    public void registerRxBus() {
        super.registerRxBus();
        addSubscribe(RxBus.getDefault().toObservable(EventInfo.class)
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(eventInfo -> {
                if (eventInfo.getType() == AppConstant.EVENT_DEVICE_BIND_SUCCESS) {
                   getContext().finish();
                }
           })
        );
    }

    @SuppressLint("CheckResult")
    public void getDeviceType() {
        addSubscribe( HttpClient.Companion.getInstance().getApiService()
                .getSmartDeviceType()
                .compose(RxUtils.INSTANCE.compose())
                .subscribeWith(new ApiResponse<SmartDeviceWrap>() {

                    @Override
                    public void success(SmartDeviceWrap data,  String msg) {
                        LogUtils.d("success");
                        deviceTypeData.addData(data.getList());
                    }

                    @Override
                    public void onFail(Throwable e, String errorMsg) {
                        LogUtils.d(errorMsg);
                    }

                })
        );

    }
}
